//
//  XOShopInfomationController.m
//  didigo
//
//  Created by JS_Coder on 2019/2/20.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOShopInfomationController.h"
#import "XOShopViewModel.h"
#import "XOShopModel.h"
#import "XOShopInfoRequest.h"
#import "XOShopInfoCell.h"
@interface XOShopInfomationController()<UITableViewDelegate, UITableViewDataSource>{
    NSString * _infoCellIdentifier;
}
StrongProperty UITableView *tableView;
StrongProperty UIView* topView;
StrongProperty UIImageView *shopBackgroundImageView;
StrongProperty UIImageView *shopLogoImageView;
StrongProperty UILabel *shopNameLabel;
StrongProperty UITextView *shopDescriptionTextView;
StrongProperty XOShopModel *shopModel;
StrongProperty UIView *lineView;
StrongProperty UIView *titleView;

@end
@implementation XOShopInfomationController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - 懒加载
-(UITableView *)tableView {
    if (_tableView != NULL) {
        return _tableView;
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, self.view.height - NavigationBarHeight - TabBarHeight) style:UITableViewStyleGrouped];
    [_tableView registerNib:[UINib nibWithNibName:@"XOShopInfoCell" bundle:nil] forCellReuseIdentifier:_infoCellIdentifier];
    _tableView.rowHeight = 44;
    _tableView.sectionHeaderHeight = 35;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = UIColor.whiteColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    return _tableView;
}

- (UIView *)topView {
    if (_topView != NULL) {
        return _topView;
    }
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    return _topView;
}

- (UIView *)lineView {
    if (_lineView != NULL) {
        return _lineView;
    }
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.shopDescriptionTextView.frame) + CommonMargin, ScreenWidth, 10)];
    _lineView.backgroundColor = UIColorFromRGB(0xF5F5F5);
    return _lineView;
}

- (UIImageView *)shopBackgroundImageView {
    if (_shopBackgroundImageView != NULL) {
        return _shopBackgroundImageView;
    }
    _shopBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.topView.width, 160)];
    return _shopBackgroundImageView;
}

-(UIImageView *)shopLogoImageView {
    if (_shopLogoImageView != NULL) {
        return _shopLogoImageView;
    }
    CGFloat WH = 80;
    _shopLogoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(( self.topView.width - WH ) * 0.5, CGRectGetMaxY(self.shopBackgroundImageView.frame) - WH * 0.5, WH, WH)];
    _shopLogoImageView.layer.cornerRadius = WH * 0.5;
    _shopLogoImageView.layer.borderColor = UIColor.whiteColor.CGColor;
    _shopLogoImageView.layer.borderWidth = 5;
    _shopLogoImageView.layer.shadowColor = UIColor.darkGrayColor.CGColor;
    _shopLogoImageView.layer.shadowOffset = CGSizeMake(0, 3);
    _shopLogoImageView.layer.shadowRadius = 5;
    _shopLogoImageView.layer.shadowOpacity = 0.8;
    _shopLogoImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:_shopLogoImageView.bounds].CGPath;
    _shopLogoImageView.layer.masksToBounds = YES;
    return _shopLogoImageView;
}

-(UILabel *)shopNameLabel {
    if (_shopNameLabel != NULL) {
        return _shopNameLabel;
    }
    _shopNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.shopLogoImageView.frame) + CommonMargin, self.topView.width, 25)];
    _shopNameLabel.textAlignment = NSTextAlignmentCenter;
    _shopNameLabel.textColor = AppTextColor;
    _shopNameLabel.font = CHINESE_PingFangRegular(15);
    _shopNameLabel.layer.cornerRadius = 25 * 0.5;
    _shopNameLabel.layer.borderColor = AppTextColor.CGColor;
    _shopNameLabel.layer.borderWidth = 0.2;
    _shopNameLabel.layer.masksToBounds = YES;
    return _shopNameLabel;
}

- (UIView *)titleView {
    UIView *baseV = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.shopNameLabel.frame) + CommonMargin, ScreenWidth, 35)];
    baseV.backgroundColor = UIColor.whiteColor;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CommonMargin, 5, ScreenWidth, 25)];
    label.text = @"店铺介绍";
    label.textColor = AppTextColor;
    label.font = CHINESE_PingFangMedium(16);
    [baseV addSubview:label];
    _titleView = baseV;
    return _titleView;
}

- (UITextView *)shopDescriptionTextView {
    if (_shopDescriptionTextView != NULL) {
        return _shopDescriptionTextView;
    }

    _shopDescriptionTextView = [[UITextView alloc]initWithFrame:CGRectMake(CommonMargin, CGRectGetMaxY(self.titleView.frame) + CommonMargin, self.topView.width - (2 * CommonMargin), 100)];
    _shopDescriptionTextView.textColor = UIColor.lightGrayColor;
    _shopDescriptionTextView.font = CHINESE_PingFangRegular(13);
    [_shopDescriptionTextView setEditable:NO];
    _shopDescriptionTextView.layer.borderWidth = 0.2;
    _shopDescriptionTextView.layer.borderColor = AppTextColor.CGColor;
    _shopDescriptionTextView.layer.cornerRadius = 3;
    _shopDescriptionTextView.layer.masksToBounds = YES;
    return _shopDescriptionTextView;
}

#pragma Setter 属性
- (void)setShopModel:(XOShopModel *)shopModel {
    if (_shopModel != shopModel) {
        _shopModel = shopModel;
        [self setTopShopInfo];
        [self.tableView reloadData];
    }
}

#pragma mark - 初始化
- (void)configOwnViews {
    XOShopModel *model = [XOShopViewModel shared].shopModel;
    _infoCellIdentifier = @"infoCellIdentifier";
    [self configNavigationLeftItemWithImage:[UIImage imageNamed:@"back_icon"]];
    self.navigationItem.title = model.shopName;
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.topView];
    [self.topView addSubview:self.shopBackgroundImageView];
    [self.topView addSubview:self.shopLogoImageView];
    [self.topView addSubview:self.shopNameLabel];
    [self.topView addSubview:self.titleView];
    [self.topView addSubview:self.shopDescriptionTextView];
    [self.topView addSubview:self.lineView];

    self.topView.frame = CGRectMake(0, -CGRectGetMaxY(self.lineView.frame), self.topView.width, CGRectGetMaxY(self.shopDescriptionTextView.frame));
    self.tableView.contentInset = UIEdgeInsetsMake(self.topView.height + 20, 0, 0, 0);
    
    [self.shopLogoImageView setImageWithUrl:model.logo];
    self.shopNameLabel.text = model.shopName;
    // 获取商品信息
    [self getShopDetailInfo];
}


- (void)setTopShopInfo {
    if (self.shopModel) {
            //set topInfo
        [self.shopBackgroundImageView setImageWithUrl:self.shopModel.bg];
        [self.shopLogoImageView setImageWithUrl:self.shopModel.logo];
        CGFloat nameWitdh = [self.shopModel.shopName boundingRectWithSize:CGSizeMake(ScreenWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: CHINESE_PingFangMedium(15)} context:nil].size.width + CommonMargin;
        [self.shopNameLabel setMj_x:(ScreenWidth - nameWitdh) * 0.5];
        [self.shopNameLabel setWidth:nameWitdh];
        self.shopNameLabel.text = self.shopModel.shopName;
        self.shopDescriptionTextView.text = self.shopModel.intro;
    }
}

- (void)leftBarButtonAction:(id)sender {
    [self.tabBarController.navigationController popViewControllerAnimated:YES];
}

#pragma mark - loadData
#pragma mark  获取店铺详情
- (void)getShopDetailInfo{
    XOShopModel *model = [XOShopViewModel shared].shopModel;
    XOShopInfoRequest *req = [XOShopInfoRequest new];
    req.shopId = model.shopId;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        self.shopModel = response.data;
    } failHandler:^(BaseResponse *response) {

    }];
}

#pragma mark UITableViewDelegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XOShopInfoCell* cell = [tableView dequeueReusableCellWithIdentifier:_infoCellIdentifier forIndexPath:indexPath];
    if (self.shopModel) {
        switch (indexPath.row) {
            case 0:
                cell.infoNameLabel.text = @"电话:";
                cell.infoValueLabel.text = self.shopModel.tel;
                break;
            case 1:
                cell.infoNameLabel.text = @"邮箱:";
                cell.infoValueLabel.text = self.shopModel.email;
                break;
            case 2:
                cell.infoNameLabel.text = @"地址:";
                cell.infoValueLabel.text = self.shopModel.address;
                break;
            default:
                break;
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *baseV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    baseV.backgroundColor = UIColor.whiteColor;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CommonMargin, 5, ScreenWidth, 25)];
    label.text = @"基本信息";
    label.textColor = AppTextColor;
    label.font = CHINESE_PingFangMedium(16);
    [baseV addSubview:label];
    return baseV;
}



@end
